package com.zbbt.sportslottery.ui.fragment;

import android.annotation.TargetApi;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.os.Build;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.app.App;
import com.zbbt.sportslottery.app.Constant;
import com.zbbt.sportslottery.app.ServerConfig;
import com.zbbt.sportslottery.bean.UserInfo;
import com.zbbt.sportslottery.net.TypeGsonHttpHandler;
import com.zbbt.sportslottery.net.ZbHttpClient;
import com.zbbt.sportslottery.ui.activity.LoginActivity;
import com.zbbt.sportslottery.ui.activity.MainActivity;
import com.zbbt.sportslottery.ui.widget.CircleImageView;
import com.zbbt.sportslottery.util.LogUtil;
import com.zbbt.sportslottery.util.Md5;
import com.zbbt.sportslottery.util.SharePreferUtil;
import com.zbbt.sportslottery.util.UmengUtil;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class LeftMenuFragment extends SimpleBaseFragment {
	/** 开奖公告 */
	private LinearLayout drawNotice;
	/** 活动中心 */
	private LinearLayout actionCenter;
	/** 设置 */
	private LinearLayout seting;
	/** 赛事数据 */
	private LinearLayout eventData;
	/** 首页 */
	private LinearLayout home;
	/** 附近的体彩店 */
	private LinearLayout lotteryShop;
	/** 登录/注册 */
	private LinearLayout login;
	/** 发现 */
	private LinearLayout faXian;
	/** 11选5走势图️ */
	private RelativeLayout elevenWebWiew;
	/** 体验卷认证 */
	private LinearLayout saoyiao;
	/** 体验卷记录 */
	private LinearLayout experienceRecord;
	/** 店主登陆时 显示的View */
	private LinearLayout dianzhu_View;
	private CircleImageView user_head_pic;
	private TextView toLog;
	private TextView name;
	private TextView score;
	private String nameStr = "";
	private String socreStr = "";
	private ImageView K3_img;
	private MainActivity mainActivity;

	@Override
	protected void initView(LayoutInflater inflater) {
		mView = inflater.inflate(R.layout.fragment_left_menu, null);
		login = (LinearLayout) findViewById(R.id.log_regist);
		home = (LinearLayout) findViewById(R.id.home);// 首页
		drawNotice = (LinearLayout) findViewById(R.id.draw_notice);// 开奖公告
		actionCenter = (LinearLayout) findViewById(R.id.action_center);// 活动中心
		seting = (LinearLayout) findViewById(R.id.seting);// 设置
		eventData = (LinearLayout) findViewById(R.id.event_data);// 赛事数据
		faXian = (LinearLayout) findViewById(R.id.faxian_notice);
		elevenWebWiew = (RelativeLayout) findViewById(R.id.eleven_layout);
		saoyiao = (LinearLayout) findViewById(R.id.saoyisao_layout);// 体验券认证
		experienceRecord = (LinearLayout) findViewById(R.id.exchange_record_layout);// 体验券记录
		toLog = (TextView) findViewById(R.id.to_log);
		name = (TextView) findViewById(R.id.name);
		score = (TextView) findViewById(R.id.score);
		K3_img = (ImageView) findViewById(R.id.fx_red_ball);
		lotteryShop = (LinearLayout) findViewById(R.id.lottery_shop);
		mainActivity = (MainActivity) getActivity();
		dianzhu_View = (LinearLayout) findViewById(R.id.dianzhu_view);
		dianzhu_View.setVisibility(View.GONE);
		user_head_pic = (CircleImageView) findViewById(R.id.user_head_pic);
	}

	@Override
	protected void setListener() {
		login.setOnClickListener(this);
		home.setOnClickListener(this);
		drawNotice.setOnClickListener(this);
		actionCenter.setOnClickListener(this);
		seting.setOnClickListener(this);
		eventData.setOnClickListener(this);
		faXian.setOnClickListener(this);
		elevenWebWiew.setOnClickListener(this);
		lotteryShop.setOnClickListener(this);
		saoyiao.setOnClickListener(this);
		experienceRecord.setOnClickListener(this);
	}

	@Override
	protected void onClickEvent(View v) {
//		switch (v.getId()) {
//		case R.id.log_regist: {// 登录注册
//			if (!App.isLogin) {
//				goActivity(LoginActivity.class);
//				UmengUtil.onEvent(context, Constant.UMENG_LOGIN);
//			} else {
//				mainActivity.changeFragment(10);
//			}
//			break;
//		}
//		case R.id.home: {// 资讯中心
//			mainActivity.changeFragment(0);
//			UmengUtil.onEvent(context, Constant.UMENG_NEWCENTER);
//			break;
//		}
//		case R.id.draw_notice: {// 开奖公告
//			mainActivity.changeFragment(1);
//			UmengUtil.onEvent(context, Constant.UMENG_OPENCENTER);
//			break;
//		}
//		case R.id.action_center: {// 活动中心
//			mainActivity.changeFragment(2);
//			UmengUtil.onEvent(context, Constant.UMENG_ACCENTER);
//			break;
//		}
//		case R.id.lottery_shop: {// 附近彩票店
//			App.isLotteryShopFragment = true;
//			mainActivity.changeFragment(4);
//			UmengUtil.onEvent(context, Constant.UMENG_MACTH);
//			break;
//		}
//		case R.id.event_data: {// 赛事数据
//			mainActivity.changeFragment(3);
//			UmengUtil.onEvent(context, Constant.UMENG_MACTH);
//			break;
//		}
//		case R.id.seting: {// 设置
//			mainActivity.changeFragment(5);
//			UmengUtil.onEvent(context, Constant.UMENG_SETTING);
//			break;
//		}
//		case R.id.faxian_notice: {// 发现
//			K3_img.setVisibility(View.GONE);
//			mainActivity.changeFragment(6);
//			UmengUtil.onEvent(context, Constant.UMENG_FAXIAN);
//			break;
//		}
//		case R.id.eleven_layout: {// 图表
//			mainActivity.changeFragment(7);
//			UmengUtil.onEvent(context, Constant.UMENG_ZOUSHITU);
//			break;
//		}
//		case R.id.saoyisao_layout: {// 体验券认证（店主）
//			mainActivity.changeFragment(8);
//			break;
//		}
//		case R.id.exchange_record_layout: {// 体验券记录（店主）
//			App.isExchangeRecordFragment = true;
//			mainActivity.changeFragment(9);
//			break;
//		}
//		}
	}

	@Override
	public void onResume() {
		super.onResume();
		LogUtil.defLog("left_frag_onResume");
		String userName = SharePreferUtil.getString(Constant.USER_NAME, "");
		String password = SharePreferUtil.getString(Constant.PASSWORD, "");
		LogUtil.printI(TAG, "");
		if (!TextUtils.isEmpty(userName) && !TextUtils.isEmpty(password)) {
			toLog.setText("自动登录中");
			httpData(userName, password);
		}
		IntentFilter filter = new IntentFilter();
		filter.addAction(Constant.INTENT_ACTION_LOGIN_SUCCESS);
		filter.addAction(Constant.INTENT_ACTION_CHANGE_LOGIN);
		context.registerReceiver(broadcastReceiver, filter);
		ImageLoader.getInstance().displayImage(SharePreferUtil.getString(Constant.USER_PHOTO_IMG, "0"),
				user_head_pic, App.option, new ImageLoadingListener() {

					@Override
					public void onLoadingStarted(String imageUri, View view) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
						// TODO Auto-generated method stub
						((CircleImageView) view).setImageResource(R.drawable.left_menu_head);
					}

					@Override
					public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onLoadingCancelled(String imageUri, View view) {
						// TODO Auto-generated method stub

					}
				});
		UmengUtil.onPageStart(TAG);
	}

	@Override
	public void onPause() {
		super.onPause();
		UmengUtil.onPageEnd(TAG);
	}

	@Override
	public void onDestroy() {
		context.unregisterReceiver(broadcastReceiver);
		super.onDestroy();
	}

	@Override
	protected void initData() {

	}

	/**
	 * 接收登陆，注册的广播
	 */
	private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (Constant.INTENT_ACTION_LOGIN_SUCCESS.equals(intent.getAction())) {
				setUserInfo();
			} else if (Constant.INTENT_ACTION_CHANGE_LOGIN.equals(intent.getAction())) {
				toLog.setVisibility(View.VISIBLE);
				name.setVisibility(View.GONE);
				score.setVisibility(View.GONE);
				toLog.setText(R.string.log_and_regist);
			}

		}

	};

	/**
	 * 登录成功后改变显示内容
	 */
	public void setUserInfo() {
		nameStr = SharePreferUtil.getString(Constant.USER_NAME, "");
		socreStr = SharePreferUtil.getString(Constant.USER_SCORE, "0");
		if (!TextUtils.isEmpty(nameStr)) {
			toLog.setVisibility(View.GONE);
			name.setVisibility(View.VISIBLE);
			score.setVisibility(View.VISIBLE);
			name.setText(nameStr);
			score.setText("彩豆:" + socreStr);
		}

	}

	/**
	 * 自动登录
	 * 
	 * @param usernameContent
	 * @param passwordContent
	 */
	private void httpData(final String usernameContent, final String passwordContent) {
		JsonObject jsonObject = new JsonObject();
		jsonObject.addProperty("mobile", usernameContent);
		jsonObject.addProperty("password", Md5.getMD5(passwordContent));
		jsonObject.addProperty("loginType", "1");
		ZbHttpClient.postNewApi(ServerConfig.API_LOGIN, jsonObject, new TypeGsonHttpHandler<UserInfo>(true) {

			@Override
			public void onSuccess(UserInfo data) {
				if (data != null) {
					name.setText(usernameContent);
					score.setText("彩豆:" + data.getTotalSource());
					name.setVisibility(View.VISIBLE);
					score.setVisibility(View.VISIBLE);
					toLog.setVisibility(View.GONE);
					App.isLogin = true;
				}
				if (App.isLogin) {
					// 2是客户 1 是店主
					String userType = SharePreferUtil.getString(Constant.USER_TYPE, "");
					if (userType.equals("2")) {
						dianzhu_View.setVisibility(View.GONE);
					} else if (userType.equals("1")) {
						dianzhu_View.setVisibility(View.VISIBLE);
					}
					setUserInfo();
				}
			}

			@Override
			public void onFailure(String errorCode, String errorMsg) {
				toLog.setText("注册 ｜ 登录");
			}
		});
	}
}
